/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uploadB1;

import com.opensymphony.xwork2.ActionSupport;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Danicela
 */
public class UploadAction extends ActionSupport {

    private String ficFileName;
    private File fic;
    private String outputPlace = "C:\\Users\\Danicela\\Downloads\\apache-tomcat-6.0.29-windows-x86\\apache-tomcat-6.0.29\\work\\Catalina\\localhost\\StrutsTP\\upload\\";
    private String outputFileLink = "http://localhost:8080/StrutsTP/upload/";
    //private ? fileContentType;

    public void setFic(File fic) {
        this.fic = fic;
    }

    public void setFicFileName(String ficFileName) {
        this.ficFileName = ficFileName;
    }

    public String getOutputFileLink() {
        return outputFileLink;
    }
    
    public String execute() {
        //marche pas
        /*
        if (fic.renameTo(new File("C:\\test\\" + ficFileName))) {
            System.out.println("gg");
        } else {
            System.out.println("t nul");
        }*/

        System.out.println(ficFileName);

        String result = ERROR;

        //get servlet context => getRealPath(java.lang.String path) voir parchy 5 - 201

        //copie de fichier : marche
        //File newFic = new File("C:\\test\\" + ficFileName);
        File newFic = new File(outputPlace + ficFileName);
        try {
            InputStream in = new FileInputStream(fic);
            OutputStream out = new FileOutputStream(newFic);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();

            outputFileLink += ficFileName;
            result = SUCCESS;

        } catch (FileNotFoundException ex) {
            System.out.println(ex);
        } catch (IOException ex) {
            System.out.println(ex);
        }

        return result ;
    }
}
